//
//  RecommendViewController.swift
//  DouYuZB
//
//  Created by 刘耀 on 2017/4/12.
//  Copyright © 2017年 深圳多诺信息科技有限公司. All rights reserved.
//

import UIKit

// MARK: - 常量
private let kCycleViewH: CGFloat  = kScreenW * 3 / 8
private let kGameViewH : CGFloat = 90


/// 推荐界面
class RecommendViewController: BaseAnchorViewController {
    
    // MARK: - 懒加载函数
    fileprivate lazy var recommendVM: RecommendViewModel = RecommendViewModel()
    fileprivate lazy var cycleView: CycleView = {
       let cycleView = CycleView.cycleView()
        cycleView.frame = CGRect(0, -(kCycleViewH + kGameViewH), kScreenW, kCycleViewH)
        return cycleView
        
    }()
    fileprivate lazy var gameView : RecommendGameView = {
        let gameView = RecommendGameView.recommendGameView()
        gameView.frame = CGRect(x: 0, y: -kGameViewH, width: kScreenW, height: kGameViewH)
        return gameView
    }()
    
}


// MARK: - 设置UI界面内容
extension RecommendViewController {
    override func setupUI() {
        super.setupUI()
        collectionView.addSubview(cycleView)
        collectionView.addSubview(gameView)
        
        
        // 设置collectionView的内边距
        collectionView.contentInset = UIEdgeInsetsMake(kCycleViewH + kGameViewH, 0, 0, 0)
        
    }
}

// MARK: - 请求数据
extension RecommendViewController {
    override func loadData() {
        
        baseVM = recommendVM
        
        // 请求推荐数据 没有循环引用。对象没有对闭包强引用
        recommendVM.requestData { 
            self.collectionView.reloadData()
            
            var groups = self.recommendVM.anchorGroups
            // 2.1.移除前两组数据
            groups.removeFirst()
            groups.removeFirst()
            
            // 2.2.添加更多组
            let moreGroup = AnchorGroup()
            moreGroup.tag_name = "更多"
            groups.append(moreGroup)
            
            self.gameView.groups = groups
            
            self.loadDataFinished()
        }
        // 请求轮播数据
        recommendVM.requestCycleData { 
            self.cycleView.cycleModels = self.recommendVM.cycleModels
        }
    }
}

// MARK: - UICollectionViewDataSource
extension RecommendViewController: UICollectionViewDelegateFlowLayout {
    
    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if indexPath.section == 1 {
            // 1.取出PrettyCell
            let prettyCell = collectionView.dequeueReusableCell(withReuseIdentifier: kPrettyCellID, for: indexPath) as! CollectionViewPrettyCell
            
            // 2.设置数据
            prettyCell.anchor = recommendVM.anchorGroups[indexPath.section].anchors[indexPath.item]
            
            return prettyCell
        } else {
            return super.collectionView(collectionView, cellForItemAt: indexPath)
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        if indexPath.section == 1 {
            return CGSize(kNormalItemW, kPrettyItemH)
        }
        return CGSize(kNormalItemW, kNormalItemH)
    }
    
}
